package com.saifan.wyy_ov.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * 软键盘管理工具类
 *
 * @author Administrator
 *
 */
public class SoftUtil {
	/**
	 * @MethodName: checkInput
	 * @Description: 检测输入法是否弹出
	 * @param activity
	 * @return
	 */
	public static boolean checkInput(Activity activity){
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		return imm.isActive();
	}

	// 隐藏软键盘
	public static void hideSoftInput(Activity activity) {
		activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	/**
	 * @MethodName: showSoftInput
	 * @Description: 强制显示键盘
	 * @param activity
	 * @param view
	 */
	public static void showSoftInput(final Activity activity, final View view) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);// 强制显示键盘

	}

	/**
	 * @MethodName: showSoftInput
	 * @Description: 强制显示键盘到View
	 * @param activity
	 * @param view
	 * @param time
	 *            时间
	 */
	public static void showSoftInput(final Activity activity, final View view, final long time) {
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);// 强制显示键盘
			}
		}, time);

	}

	/**
	 * @MethodName: hideSoftInput
	 * @Description: 强制隐藏键盘
	 * @param activity
	 * @param view
	 */
	public static void hideSoftInput(final Activity activity, final View view) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
	}

	/**
	 * @MethodName: hideSoftInput
	 * @Description: 强制隐藏键盘
	 * @param activity
	 * @param view
	 * @param time
	 */
	public static void hideSoftInput(final Activity activity, final View view, final int time) {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
			}
		}, time);
	}

}
